using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Net;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FlickrNet;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class RandomRecent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Flickr.CacheLocation = Server.MapPath(".");
        Flickr.CacheDisabled = true;
        Flickr fl = new Flickr("0d45a3f7cf287a413e8fa372a08f5966");
        Photos ps = fl.PhotosSearch("88001117@N00", "rnd");
        //Response.Write(fl.PeopleFindByEmail("settinghead@hotmail.com").UserId+"<BR>");
        //Photoset ps = fl.PhotosetsGetPhotos(album.PhotosetId);

        Random rnd = new Random();

        SortByDate(ps.PhotoCollection);

        //Photos pts = fl.PhotosSearch("88001117@N00", "rnd");

        int id = rnd.Next(10);

        //PhotoInfo info = fl.PhotosGetInfo(ps.PhotoCollection[id].PhotoId);

        HttpWebRequest httprq = (HttpWebRequest)WebRequest.Create(ps.PhotoCollection[id].LargeUrl);
        httprq.ContentType = "image/jpeg";

        HttpWebResponse httprp = (HttpWebResponse)httprq.GetResponse();

        Bitmap bmp = new Bitmap(httprp.GetResponseStream());
        Graphics g = Graphics.FromImage(bmp);

        LinearGradientBrush brush;
        Color backcolor = Color.FromArgb(220, 242, 229);
        int width = 20;
        Rectangle rectBrush = new Rectangle(0, 0, bmp.Width, width);
        brush = new LinearGradientBrush(rectBrush, backcolor,
                       Color.Transparent, LinearGradientMode.Vertical);
        g.FillRectangle(brush, rectBrush);

        rectBrush = new Rectangle(0, bmp.Height - width, bmp.Width, 20);
        brush = brush = new LinearGradientBrush(rectBrush, Color.Transparent,
                       backcolor, LinearGradientMode.Vertical);
        g.FillRectangle(brush, rectBrush);

        rectBrush = new Rectangle(0, 0, width, bmp.Height);
        brush = brush = new LinearGradientBrush(rectBrush,
                       backcolor, Color.Transparent, LinearGradientMode.Horizontal);
        g.FillRectangle(brush, rectBrush);

        rectBrush = new Rectangle(bmp.Width - width, 0, width, bmp.Height);
        brush = brush = new LinearGradientBrush(rectBrush, Color.Transparent,
                       backcolor, LinearGradientMode.Horizontal);
        g.FillRectangle(brush, rectBrush);

        Bitmap newbmp = Resize(bmp, 550, 700);
        g = Graphics.FromImage(newbmp);

        PhotoInfo pi = fl.PhotosGetInfo(ps.PhotoCollection[id].PhotoId);


        int len = pi.Title.Length < 15 ? 15 : pi.Title.Length;
        int titleSize = newbmp.Size.Width / len * 120 / 100;
        int descriptionSize;

        len = pi.Description.Length < 30 ? 30 : pi.Description.Length;
        if (pi.Description.Length == 0) descriptionSize = 0;
        else descriptionSize = newbmp.Size.Width / len * 120 /100;

        int offsetx = titleSize * 5 / 100;
        int offsety = titleSize * 8 / 100;
        offsetx = (offsetx != 0) ? offsetx : 1;
        offsety = (offsetx != 0) ? offsety : 1;

        int des_offsetx = descriptionSize * 5 / 100;
        int des_offsety = descriptionSize * 8 / 100;
        des_offsetx = (des_offsetx != 0) ? des_offsetx : 1;
        des_offsety = (des_offsetx != 0) ? des_offsety : 1;


        g.SmoothingMode = SmoothingMode.AntiAlias;

        //add title
        g.DrawString(pi.Title,
            new Font("verdana", titleSize), Brushes.Black, 5,
            newbmp.Size.Height - (titleSize + descriptionSize) * 170 / 100);
        g.DrawString(pi.Title,
            new Font("verdana", titleSize), Brushes.White, 5 - offsetx,
            newbmp.Size.Height - (titleSize + descriptionSize) * 170 / 100 - offsety);

        //add description
        if (pi.Description.Length > 0)
        {
            g.DrawString(pi.Description,
                new Font("verdana", descriptionSize), Brushes.Black, 20,
                newbmp.Size.Height - descriptionSize * 2);
            g.DrawString(pi.Description,
                new Font("verdana", descriptionSize), Brushes.Wheat, 20 - des_offsetx,
                newbmp.Size.Height - descriptionSize * 2 - des_offsety);
        }


        Response.ContentType = "image/jpeg";


        newbmp.Save(Response.OutputStream, bmp.RawFormat);

        //Response.Write(@"<a href=""" + ps.PhotoCollection[id].NewUrl + @""">Click here</a>");
        //Response.Write(album.PhotoCollection[0].LargeUrl);
    }

    public static Bitmap Resize(Bitmap loBMP, int lnWidth, int lnHeight)
    {

        ImageFormat loFormat = loBMP.RawFormat;

        decimal lnRatio;

        int lnNewWidth = 0;

        int lnNewHeight = 0;



        //*** If the image is smaller than a thumbnail just return it

        if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)

            return loBMP;

        if (loBMP.Width > loBMP.Height)
        {
            lnRatio = (decimal)lnWidth / loBMP.Width;
            lnNewWidth = lnWidth;
            decimal lnTemp = loBMP.Height * lnRatio;
            lnNewHeight = (int)lnTemp;
        }

        else
        {
            lnRatio = (decimal)lnHeight / loBMP.Height;
            lnNewHeight = lnHeight;
            decimal lnTemp = loBMP.Width * lnRatio;
            lnNewWidth = (int)lnTemp;
        }



        // System.Drawing.Image imgOut = 

        //      loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight,

        //                              null,IntPtr.Zero);



        // *** This code creates cleaner (though bigger) thumbnails and properly
        // *** and handles GIF files better by generating a white background for
        // *** transparent images (as opposed to black)

        System.Drawing.Bitmap bmpOut = new Bitmap(lnNewWidth, lnNewHeight);

        Graphics g = Graphics.FromImage(bmpOut);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
        g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);


        //loBMP.Dispose();

        return bmpOut;
    }

    private void SortByDate(Photo[] photos)
    {
        long[] dt = new long[photos.Length];
        for (int i = 0; i < photos.Length; i++)
        {
            dt[i] = long.Parse(photos[i].PhotoId);
        }
        Array.Sort(dt, photos);
    }


}
